#include<iostream>
#include<pthread.h>
#include<unistd.h>

using namespace std;

__thread int global=100;

void* func1(void* args)
{
    sleep(1);
    pthread_t id =pthread_self();
    cout<<"tid1: "<<id<<endl;
    int cnt=0;
    while (true)
    {
        string name = (char *)args;
        cout << name << "正在运行" << endl;
        cnt++;
        if(cnt==10)
        break;
        sleep(1);

    }
    global+=cnt;
    cout<<"global 1:"<<global<<endl;
}

void* func2(void* args)
{
    sleep(2);
    pthread_t id = pthread_self();
    cout<<"tid2: "<<id<<endl;
    int cnt=0;
    while (true)
    {
        string name = (char *)args;
        cout << name << "正在运行" << endl;
        cnt++;
        if(cnt==15)
        break;
        sleep(1);
    }
    global+=cnt;
    cout<<"global 2:"<<global<<endl;

}


int main()
{
    pthread_t tid1,tid2;

    pthread_create(&tid1,nullptr,func1,(void*)"thread1");
    pthread_create(&tid2,nullptr,func2,(void*)"thread2");
    sleep(3);
    pthread_t id=pthread_self();
    cout<<"主线程: "<<id<<endl;
    int cnt=0;
    while(true)
    {
        cnt++;
        cout<<"主线程正在运行"<<endl;
        if(cnt==20)
        break;
        sleep(1);
    }
    global+=cnt;
    cout<<"global main:"<<global<<endl;
    pthread_join(tid1,nullptr);
    pthread_join(tid2,nullptr);

    return 0;
}


















// #include<iostream>
// #include<pthread.h>
// #include<unistd.h>

// using namespace std;



// int main()
// {

//     return 0;
// }

